
import argparse
import sys
import os
from core.commands import Commands
from core.project import Project
from core.config import Config
import shutil

class Builder(Commands):
 
    def __init__(self):
        Commands.__init__(self)
        self.__clean=False
 
    def main(self,source=None):
        if self.__clean:
            if os.path.exists(Config.OUT_PATH+Config.FOLDER):
                shutil.rmtree(Config.OUT_PATH+Config.FOLDER)
            return
        
        
        if Config.SOURCE:
            source=Config.SOURCE
        if source:
            Config.SOURCE=source
            Config.SRC_PATH=os.path.dirname(source)+"/"
            p=Project(Config.SOURCE,Config.OUT_PATH)
            p.build()
        else:
            print "ERROR need source"
            return True
        
    def _out(self,path=None):
        "Specified the build path (default: ./bin/js/)"
        Config.OUT_PATH=path
    
    def _folder(self,path=None):
        "package folder (default: src/)"
        Config.FOLDER=path    
    
    def _compression(self,level=1):
        """compresse javascript
            [level]: 1 (default),2 (shrink private), 3 (not usable externly)"""
        Config.COMPRESSION=level
        Config.SHRINK_VAR=True
    
    def clean(self):
        self.__clean=True
        
        
    def _merge(self):
        "merge all class in one"
        Config.MERGE_FILE=True
        
    def _debug(self):
        "add debug option"
        Config.DEBUG=True
        
    def help(self):
        ret="\n"
        ret+="usage: "+os.path.basename(__file__) +" source [options]"
        ret+="\n\n"
        ret+="source"+" "*(self._minspace-len("source"))
        ret+="as3 file or folder contain as3 files"
        ret+=""
        return ret
    
    def temp(self):
        "default build for dev"
        self.command(["../as/src/Main.as","-o","../as/bin/js"])
        return True
        
    
    

#print "usage : "+
if __name__ == '__main__':
    Builder().command(sys.argv[1:])
